package yxy.game.pm2.command.therionHunting;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.module.rank.RankModule;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.TherionHuntingMessage;
import yxy.game.pm2.module.therionHunting.TherionChallengingPosition;
import yxy.game.pm2.module.therionHunting.TherionHandle;
import yxy.game.pm2.module.therionHunting.TherionModule;
import yxy.game.pm2.module.therionHunting.TherionPlayerModule;

import java.util.Map;

/**
 * @name CMD_TherionHuntingRetreat
 * @author wrs
 * @descrition 神兽猎场撤退
 * @since 2022-06-14
 */
@MessageType(proto = Command.TherionHuntingRetreat ,classtype = TherionHuntingMessage.TherionHuntingRetreatRequest.class)
public class CMD_TherionHuntingRetreat extends Command<TherionHuntingMessage.TherionHuntingRetreatRequest> {

    @Override
    public void execute(MessageSource messageSource, TherionHuntingMessage.TherionHuntingRetreatRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int gameType = request.getGameType(); //玩法类型；1：本服神兽争夺，2：跨服神兽猎场，3：跨服世界神兽
        int therionId = request.getTherionId(); //神兽ID
        int positionId = request.getPositionId(); //挑战位Id

        TherionHuntingMessage.TherionHuntingRetreatResponse.Builder response = TherionHuntingMessage.TherionHuntingRetreatResponse.newBuilder();

        TherionHandle handle = new TherionHandle();
        try {
            //神兽类型
            int therionType = handle.getTherionKey(therionId);
            if (therionType == -1){
                send(messageSource,response, Result.DataError);
                return;
            }

            //神兽信息
            TherionModule therionModule = TherionModule.get(gameType, playerCode, therionType);
            if (therionModule == null){
                send(messageSource,response, Result.DataError);
                return;
            }

            //排行榜
            RankModule rankModule = handle.getRankModule(gameType, playerCode, therionType);
            if (rankModule == null){
                send(messageSource,response, Result.DataError);
                return;
            }

            long initialLife = therionModule.getTotalLife();//神兽初始血量
            //玩家撤退处理
            Result retreatHandleResult = handle.retreatHandle(gameType,playerCode,therionType,initialLife,therionModule,rankModule,positionId);
            if (!retreatHandleResult.equals(Result.Success)){
                send(messageSource,response, Result.DataError);
                return;
            }
            Integer challengeNum = TherionPlayerModule.get(playerCode).getChallengeNum().get(gameType);
            response.setChallengNum(challengeNum);

            //神兽的所有挑战位
            Map<Integer, TherionChallengingPosition> positionMap = TherionChallengingPosition.getAll(gameType, playerCode,therionType);
            if (positionMap == null){
                send(messageSource,response, Result.DataError);
                return;
            }

            Result packTherionBattleUiResult = handle.packTherionBattleUiInfo(playerCode,therionModule,positionMap,rankModule,response);
            if (!packTherionBattleUiResult.equals(Result.Success)){
                send(messageSource,response,packTherionBattleUiResult);
                return;
            }

            send(messageSource,response, Result.Success);

        }catch (Exception e){
            e.printStackTrace();
            send(messageSource,response, Result.DataError);
        }

    }

    private void send(MessageSource messageSource, TherionHuntingMessage.TherionHuntingRetreatResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
